<?php

/**
 * NOTICE OF LICENSE
 *
 * THIS SOURCE FILE IS PART OF EVEBIT'S PRIVATE PROJECT.
 * 
 * DO NOT USE THIS FILE IN OTHER PLACE.
 *
 * @category    EveBit_Library
 * @package     Application
 * @author      Cheng Wei <berlios.o@gmail.com>
 * @version     $$Id: Backend.php 1567 2011-07-07 08:01:47Z chen.hao $$
 * @copyright   Copyright (c) 2008 Evebit Inc. China (http://www.evebit.com)
 */

/**
 * Default action for backend
 * 
 * @package     Application
 * @author      Cheng Wei <berlios.o@gmail.com>
 * @version     $$Id: Backend.php 1567 2011-07-07 08:01:47Z chen.hao $$
 */
class Evebit_Controller_Action_Backend extends Evebit_Controller_Action_Public {
	
	/**
	 * filter no authentication user
	 */
	
	public function preDispatch() {
		$module = $this->getRequest ()->getModuleName ();
		$controller = $this->getRequest ()->getControllerName ();
		$action = $this->getRequest ()->getActionName ();
		$params = $this->getRequest ()->getParams ();
		
		$allowActionName = array ('index', 'edit', 'delete', 'add' );
		
		/**
		 * $allowActionParam means all item or just self id item or other range
		 */
		$allowActionParam = array ('all', 'self' );
		if (! Zend_Registry::isRegistered ( 'resource' )) {
			$resource = new Default_Model_Entity_Resource ();
			$collection = $resource->getCollection ();
			Zend_Registry::set ( 'resource', $collection );
		}
		
		//if (in_array($action, $allowActionName)) {
		$collection = Zend_Registry::get ( 'resource' );
		$resource = new Default_Model_Entity_Resource ();
		$items = $collection->getItems ();
		$isExitController = false;
		$isExitParams = false;
		foreach ( $items as $item ) {
			if ($item->getModule () == $module && $item->getController () == $controller && $item->getAction () == $action) {
				$itemparams = explode ( "|", $item->getParams );
				foreach ( $params as $keys => $values ) {
					if (in_array ( $keys, $itemparams )) {
						$isExitParams = true;
					}
				}
				if (! $isExitParams) {
					$isExitController = true;
				}
			}
		}
		if (! $isExitController) {
			if ($isExitParams) {
				$collection = $resource->getCollection ();
				$collection->addFilter ( "controller", $controller );
				$collection->addFilter ( "action", action );
			}
			$resource->setData ( "controller", $controller );
			$resource->setData ( "action", $action );
			$paramsValue = "";
			foreach ( $params as $key => $value ) {
				if (in_array ( $key, $allowActionParam )) {
					$paramsValue = $paramsValue . $key . "|";
				}
			}
			$resource->setData ( "scope", $this->getInvokeArg ( 'bootstrap' )->getOption ( 'scope' ) );
			$resource->setData ( "module", $module );
			$resource->setData ( "params", $paramsValue );
			$resource->save ();
		}
		//}
		

		if (! Zend_Auth::getInstance ()->hasIdentity ()) {
			if (($controller != 'index' || ($action != 'login' && $action != 'logout')) && $action != "upload" && $action != "callback") {
				$returnUrl = "";
				$this->_forward ( "login", "index", "default", array ("urnc" => base64_encode ( $returnUrl ) ) );
			}
		}
		/**
		 * autoAdd resource function end
		 */
		
		/**
		 * auto acl check start
		 */
		if ($controller != "index" && in_array ( $action, $allowActionName )) {
			$user = $this->getUser ();
			if (! $user->checkAcl ( $this )) {
				$this->getSession ()->addError ( $this->__ ( "You don't have permission to view this page!" ) );
				$this->_redirect ( "/" );
			}
		}
	}
}
